home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / tools / internet-tools / connect-line / cl / devkit / c / examples / words / words.c < prev   
Encoding:
C/C++ Source or Header  |  1995-11-06  |  7.8 KB  |  424 lines

  1. //
  2. // Words-Kommando für Connectline, words.c
  3. //
  4. // Copyright 1995 by Mathias Mischler
  5. //
  6.  
  7. #define HighscoreFilename "CONNECTLINE:Prefs/CL-Words.score"
  8. #define WordsFilename     "CONNECTLINE:Prefs/CL-Words.words"
  9.  
  10. #define MaxWords 2048        // Old fashing non-dynamical array
  11. #define MaxMask  2047
  12.  
  13. #define Startline 4            // First line of Playfield
  14. #define Endline 20            // Last line of Playfield
  15. #define Numberlines 16     
  16. #define Inputline 21        // Line for Input
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <cl/clonline.h>
  22.  
  23. // Version String
  24.  
  25. static char version[] = { "$VER: Words 1.2 " __AMIGADATE__ };
  26.  
  27. static long numberwords;
  28.  
  29. //
  30. // Draw_Highscore ()
  31. //
  32.  
  33. static void Draw_Highscore ( void )
  34. {
  35. BPTR lock, file;
  36. char spieler [ 10 ][ 40 ];
  37. ulong punkte [ 10 ];
  38. ulong count;
  39. char line [ 82 ];
  40.  
  41.     lock = Lock ( HighscoreFilename, ACCESS_READ );
  42.     if ( lock )
  43.     {
  44.         UnLock ( lock );
  45.         if ( file = Open ( HighscoreFilename, MODE_OLDFILE ) )
  46.         {
  47.             for ( count = 0; count < 10; count ++ )
  48.             {
  49.                 FGets ( file, line, 80 );
  50.                 line [ strlen ( line ) - 1 ] = 0; // LF entfernen
  51.                 sscanf ( line, "%d %s", &punkte [ count ], spieler [ count ] );
  52.             }
  53.             Close ( file );
  54.         }
  55.         else
  56.         {
  57.             PutStr ("Can't open highscore-file.\n");
  58.             return;
  59.         }
  60.     }
  61.     else
  62.     {
  63.         if ( file = Open ( HighscoreFilename, MODE_NEWFILE ) )
  64.         {
  65.             for ( count = 0; count < 10; count ++ )
  66.             {
  67.                 punkte [ count ] = 0;
  68.                 strcpy ( spieler [ count ], "???" );
  69.                 FPuts ( file, "0 ???\n" );
  70.             }
  71.             Close ( file );
  72.         }
  73.         else
  74.         {
  75.             PutStr ("Can't open highscore-file.\n");
  76.             return;
  77.         }
  78.     }
  79.  
  80.     PutMSG ("MSG_HIGHSCORE");
  81.     for ( count = 0; count < 10; count ++ )
  82.     {
  83.         CLONL_Printf ( MSG ("MSG_HIGHSCOREFORMAT"), punkte [ count ], spieler [ count ] );
  84.     }
  85.  
  86.     PutStr ("\n");
  87. }
  88.  
  89. //
  90. // Sortin_Score()
  91. //
  92.  
  93. static void Sortin_Score ( ulong score, char *name )
  94. {
  95. BPTR file;
  96. char spieler [ 11 ][ 40 ];
  97. ulong punkte [ 11 ];
  98. ulong count, pos;
  99. char line [ 82 ];
  100. BOOL nicht;
  101.  
  102.     nicht = TRUE;
  103.  
  104.     if ( file = Open ( HighscoreFilename, MODE_OLDFILE ) )
  105.     {
  106.         pos = 0;
  107.         for ( count = 0; count < 10; count ++ )
  108.         {
  109.             FGets ( file, line, 80 );
  110.             line [ strlen ( line ) - 1 ] = 0; // LF entfernen
  111.             sscanf ( line, "%d %s", &punkte [ pos ], spieler [ pos ] );
  112.  
  113.             if ( nicht && strcmp ( spieler [ pos ], name ) == 0 && punkte [ pos ] >= score ) break;
  114.             if ( nicht && punkte [ pos ] < score ) 
  115.             { 
  116.                 strcpy ( spieler [ pos + 1 ], spieler [ pos ] );
  117.                 punkte [ pos + 1 ] = punkte [ pos ];
  118.                 strcpy ( spieler [ pos ], name );
  119.                 punkte [ pos ] = score;
  120.                 pos ++; 
  121.                 nicht = FALSE; 
  122.             }
  123.             if ( strcmp ( spieler [ pos ], name ) == 0 ) pos --;
  124.             pos++;
  125.         }
  126.         Close ( file );
  127.     }
  128.     else
  129.     {
  130.         PutStr ("Can't open highscore-file.\n");
  131.         return;
  132.     }
  133.  
  134.     if (!nicht)
  135.     {
  136.         if ( file = Open ( HighscoreFilename, MODE_NEWFILE ) )
  137.         {
  138.             for ( count = 0; count < 10; count ++ )
  139.             {
  140.                 FPrintf ( file, "%ld %s\n", punkte [ count ], spieler [ count ] );
  141.             }
  142.             Close ( file );
  143.         }
  144.         else
  145.         {
  146.             PutStr ("Can't open highscore-file.\n");
  147.             return;
  148.         }
  149.     }
  150. }
  151.  
  152. static char *words [ MaxWords ];
  153.  
  154. //
  155. // GetWords
  156. //
  157.  
  158. static void GetWords ( void )
  159. {
  160. BPTR file;
  161. char line [ 82 ];
  162. long i = 0;
  163.  
  164.     numberwords = 0;
  165.  
  166.     file = Open ( WordsFilename, MODE_OLDFILE );
  167.  
  168.     if  ( !file )
  169.         return;
  170.  
  171.     while ( FGets ( file, line, 80 ) )
  172.     {
  173.         if ( line [ strlen ( line ) - 1 ] = 10 )
  174.             line [ strlen ( line ) - 1 ] = 0;
  175.         words [ i ] = malloc ( strlen ( line ) + 1 );
  176.         if ( !words [ i ] )
  177.         {
  178.             PutStr ( "Error getting memory.\n" );
  179.             continue;
  180.         }
  181.         strcpy ( words [ i ], line );
  182.         numberwords ++;
  183.         i++;
  184.         if ( i >= MaxWords ) 
  185.         {
  186.             PutStr ( "Error field too small.\n" );
  187.             break;
  188.         }
  189.     }
  190.  
  191.     words [ i ] = NULL;
  192.  
  193.     Close ( file );
  194. }
  195.  
  196. struct WORD
  197. {
  198.     char *word;
  199.     long line;
  200. };
  201.  
  202.  
  203. //
  204. // Spiel
  205. //
  206.  
  207. static long Spiel ( void )
  208. {
  209. long score = 0;
  210. long x, i, j;
  211. struct WORD word [ Numberlines ];
  212. char myword [ 80 ];
  213. long pause = 20, delay;
  214. long newword = 10, newdelay;
  215. long harder = 0;
  216. bool leave = FALSE;
  217. long lifes = 3;
  218.  
  219.   CLONL_DrawTitle ();
  220.     
  221.     CLONL_GotoXY ( 1, Inputline ); 
  222.  
  223.     strcpy ( myword, "" );
  224.     for ( i = 0; i < Numberlines; i ++ )
  225.     {
  226.         word[ i ].word = NULL;
  227.         word[ i ].line = 0;
  228.     }
  229.     newdelay = 0;
  230.     
  231.     while ( !leave )
  232.     {
  233.         if ( newdelay <= 0 )
  234.         {
  235.             newdelay = newword;
  236.  
  237.             j = rand ();
  238.             if ( j ) j %= numberwords;
  239.  
  240.             i = 70 - strlen ( words [ j ] );
  241.             x = rand ();
  242.             if ( x ) x %= i;
  243.  
  244.             CLONL_GotoXY ( x, Startline ); 
  245.             CLONL_Color ( COLOR_White, COLOR_Black, STYLE_Normal );
  246.  
  247.             for ( i = 0; i < Numberlines; i++ )
  248.             {
  249.                 if ( word[ i ].word == NULL )
  250.                 {
  251.                     word[ i ].word = words [ j ];
  252.                     word[ i ].line = Startline;
  253.                     PutStr ( word[ i ].word );
  254.                     break;
  255.                 }
  256.             }
  257.         }
  258.         else
  259.         {
  260.             newdelay --;
  261.         }
  262.  
  263.         delay = pause ;
  264.  
  265.         while ( !leave && delay >= 0 )
  266.         {
  267.             Delay ( 1 );
  268.             if ( CLONL_IsKeyPressed () )
  269.             {
  270.                 static long key;
  271.                 key = CLONL_GetKey ();
  272.                 switch ( key )
  273.                 {
  274.                     case CHAR_BS:
  275.                         myword [ strlen ( myword ) - 1 ] = 0;
  276.                         break;
  277.  
  278.                     case CHAR_CR:
  279.                         CLONL_GotoXY ( 2, Inputline ); 
  280.                         PutStr ( myword );
  281.                         CLONL_FlushOutput ();
  282.                         for ( i = 0; i < Numberlines; i++ )
  283.                         {
  284.                             if ( word[ i ].word && myword [ 0 ] && stricmp ( word[ i ].word, myword ) == 0 )
  285.                             {
  286.                                 CLONL_GotoXY ( 1, word[ i ].line );
  287.                                 CLONL_DeleteEOL ();
  288.                                 word[ i ].word = NULL;
  289.                                 word[ i ].line = 0;
  290.                                 newdelay = 0; // new word... hehe !
  291.  
  292.                                 score += 50;
  293.                                 harder ++ ;
  294.                                 if ( harder > 10 ) 
  295.                                 {
  296.                                     if ( newword > 1 ) newword--;    
  297.                                     harder = 0;
  298.                                 }
  299.                             }
  300.                         }
  301.                         strcpy ( myword, "" );
  302.                         break;
  303.  
  304.                     case CHAR_EOF:
  305.                     case CHAR_BREAK:
  306.                         leave = TRUE;
  307.                         break;
  308.  
  309.                     default:
  310.                         myword [ strlen ( myword ) + 1 ] = 0;
  311.                         myword [ strlen ( myword ) ] = key;
  312.                         break;
  313.                 }
  314.             }
  315.             delay --;
  316.         }
  317.  
  318.         score ++;        
  319.  
  320.         // Scroll & Draw new
  321.         CLONL_GotoXY ( 1, Inputline ); 
  322.         CLONL_DeleteEOL ();
  323.  
  324.         CLONL_GotoXY ( 1, Startline - 1 ); 
  325.         CLONL_Printf ( MSG ("MSG_SCORE"), score );
  326.  
  327.         CLONL_GotoXY ( 1, Endline ); 
  328.         CLONL_DeleteEOL ();
  329.  
  330.         CLONL_GotoXY ( 1, Startline ); 
  331.         CLONL_InsertLine ();
  332.  
  333.         for ( i = 0; i < Numberlines; i ++ )
  334.         {
  335.             if ( word[ i ].line )
  336.             {
  337.                 word[ i ].line ++;
  338.                 if ( word[ i ].line >= Endline )    // Lost
  339.                 {
  340.                     word[ i ].word = NULL;
  341.                     word[ i ].line = 0;
  342.                     lifes --;
  343.                     if ( lifes <= 0 )
  344.                     {
  345.                         leave = TRUE;
  346.                         CLONL_GotoXY ( 25, Startline + Numberlines / 2 );
  347.                         CLONL_Color ( COLOR_Red, COLOR_Black, STYLE_Bold );
  348.                         PutStr ( "-G-A-M-E---O-V-E-R-" );    
  349.                     }
  350.                 }
  351.             }
  352.         }
  353.  
  354.         CLONL_GotoXY ( 70, Inputline ); 
  355.         CLONL_Color ( COLOR_Blue, COLOR_Black, STYLE_Bold );
  356.         for ( i = 0; i < lifes; i++ )
  357.             PutStr ( "*" );
  358.         CLONL_GotoXY ( 1, Inputline ); 
  359.         CLONL_Color ( COLOR_Green, COLOR_Black, STYLE_Bold );
  360.         PutStr ( ">" ); 
  361.         CLONL_Color ( COLOR_Yellow, COLOR_Black, STYLE_Bold );
  362.         PutStr ( myword );
  363.  
  364.         CLONL_FlushOutput ();
  365.     }
  366.  
  367.     Delay ( 60 );
  368.  
  369.     return score;
  370. }
  371.  
  372. //
  373. // main
  374. //
  375.  
  376. int main ( int argc, char *argv[] )
  377. {
  378. struct Shortmenu *mymenu;
  379. char key;
  380. BOOL quit = FALSE;
  381. long score;
  382.  
  383.     GetWords ();
  384.     if ( !words ) 
  385.     {
  386.         PutStr ( "Error loading words...\nInform System-Operator!\n" );
  387.         return 0;
  388.     }
  389.  
  390.     if ( !CLONL_Open ( "Online_Words.clcat" ) ) { PutStr ( "Can't init ANSI-System.\n" ); return 20; }
  391.  
  392.     if ( CLONL_IsTTY () ) { PutStr ("You need at least VT100 or ANSI Terminal to run.\n" ); goto Ende; }
  393.  
  394.     mymenu = CLONL_AddShortMenu ( NULL, MSG("MSG_SPIELEN"), MSG("MSG_SPIELENHOT")[ 0 ] );
  395.     mymenu = CLONL_AddShortMenu ( mymenu, MSG("MSG_QUIT"), MSG("MSG_QUITHOT")[ 0 ] );
  396.  
  397.     while ( !quit )
  398.     {
  399.       CLONL_DrawTitle ();
  400.         PutStr ("Copyright 1995 by Mathias Mischler\n\n");
  401.         Draw_Highscore ();
  402.  
  403.         key = CLONL_ShortMenuSelect ( mymenu );
  404.  
  405.         switch ( key )
  406.         {
  407.             case 'Q':
  408.             case 0:
  409.                 quit = TRUE;
  410.                 break;
  411.             case 'S':
  412.                 srand ( time ( 0 ) );
  413.                 score = Spiel ();
  414.                 Sortin_Score ( score, CLONL_GetUserShell()->user.Username );
  415.                 break;
  416.         }
  417.     }
  418.     
  419.     PutStr ("\n");
  420.     CLONL_FreeShortMenu ( mymenu );
  421.     Ende:
  422.     CLONL_Close ();
  423. }
  424.